package org.lyd.spring.tx.service.impl;

import org.lyd.spring.tx.bean.Account;
import org.lyd.spring.tx.bean.Book;
import org.lyd.spring.tx.dao.AccountDao;
import org.lyd.spring.tx.dao.BookDao;
import org.lyd.spring.tx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionManager;

import java.math.BigDecimal;

/**
 * @Author:lyd
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    BookDao bookDao;

    @Autowired
    AccountDao accountDao;
    @Autowired
    TransactionManager transactionManager;

    @Override
    public void checkout(String name, Integer id, Integer num) {
        //根据id查询图书总价
        Book book = bookDao.queryBookById(id);
        BigDecimal price = book.getPrice();
        BigDecimal totalPrice = price.multiply(new BigDecimal(num));

        //更新账户余额
        accountDao.deductionAccountByUsername(name, totalPrice);

        //更新图书数量
        bookDao.updateBook(id, num);
    }
}
